package com.zlm.user.partner.controller;

import com.zlm.common.base.BaseController;
import com.zlm.common.constants.CodeConstants;
import com.zlm.common.dto.HxUserDto;
import com.zlm.common.dto.JsonResultDto;
import com.zlm.common.dto.userPartner.SysPartnerDto;
import com.zlm.common.util.MD5Utils;
import com.zlm.user.partner.service.SysPartnerService;
import com.zlm.user.partner.service.UserRelationService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 合伙人controller-（内部服务）
 * @Author: duyufu
 * @Date: 2019/7/10 11:13
 */
@RestController
@RequestMapping("/part")
public class SysPartnerController extends BaseController {

    @Autowired
    private SysPartnerService sysPartnerService;

    @Autowired
    private UserRelationService userRelationService;

    @GetMapping("/isExistPart")
    public JsonResultDto isExistPart(String mobile) {
        boolean b;
        try {
            b = sysPartnerService.isExistPart(mobile);
            return success(b);
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 查询合伙人
     * @param mobile
     * @param password
     * @return
     */
    @GetMapping(value = "/queryPartByMobileAndPassword")
    public JsonResultDto queryPartByMobileAndPassword(String mobile, String password) {
        try {
            Map<String,Object> map = new HashMap<>(2);
            map.put("mobile",mobile);
            map.put("password", MD5Utils.getStringMD5(password));
            SysPartnerDto partnerDto = sysPartnerService.queryPartner(map);
            if (null == partnerDto) {
                return fail(CodeConstants.CODE_10004);
            }
            return success(partnerDto);
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 根据验证码修改密码
     * @param mobile
     * @param password
     * @return
     */
    @GetMapping("/updatePassword")
    public JsonResultDto updatePassword(String mobile, String password) {
        try {
            sysPartnerService.updatePassword(mobile, password);
            return success();
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 根据token修改密码
     * @param partId
     * @param oldPassword
     * @param newPassword
     * @return
     */
    @GetMapping("/updatePasswordByToken")
    public JsonResultDto updatePasswordByToken(Integer partId,String oldPassword,String newPassword) {
        try {
            String password = sysPartnerService.queryPartnerPassword(partId);
            oldPassword = MD5Utils.getStringMD5(oldPassword);
            if (StringUtils.isBlank(password) || !password.equals(oldPassword)) {
                return fail(CodeConstants.CODE_10004);
            }
            newPassword = MD5Utils.getStringMD5(newPassword);
            sysPartnerService.updatePasswordByPartId(partId,newPassword);
            return success();
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 设置密码
     * @param partId
     * @param password
     * @return
     */
    @GetMapping("/setPassword")
    public JsonResultDto setPassword(Integer partId,String password) {
        try {
            sysPartnerService.setPassword(partId,password);
            return success();
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 修改手机号
     * @param partId
     * @param mobile
     * @return
     */
    @GetMapping("/changePhone")
    public JsonResultDto changePhone(Integer partId,String mobile) {
        try {
            sysPartnerService.changePhone(partId,mobile);
            return success();
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 添加好友
     * @param userId
     * @param friendHxId
     * @return
     */
    @GetMapping("/addFriend")
    public JsonResultDto addFriend(Integer userId,String friendHxId) {
        try {
//            isExistedHxUser(userHxId);
            isExistedHxUser(friendHxId);
            userRelationService.addFriend(userId,friendHxId);
            return success();
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 删除好友
     * @param userId
     * @param friendHxId
     * @return
     */
    @GetMapping("/deleteFriend")
    public JsonResultDto deleteFriend(Integer userId,String friendHxId) {
        try {
//            isExistedHxUser(userHxId);
            isExistedHxUser(friendHxId);
            userRelationService.deleteFriend(userId,friendHxId);
            return success();
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 获取单个用户
     * @param userHxId
     * @return
     */
    @GetMapping("/queryHxUser")
    public JsonResultDto queryHxUser(String userHxId) {
        try {
            HxUserDto userDto = sysPartnerService.queryHxUser(userHxId);
            return success(userDto);
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 查询好友列表
     * @param userId
     * @return
     */
    @GetMapping("/queryFriendList")
    public JsonResultDto queryFriendList(Integer userId) {
        try {
            List<HxUserDto> friendList = userRelationService.queryFriendList(userId);
            return success(friendList);
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 搜索好友
     * @param keyword
     * @return
     */
    @GetMapping("/searchFriend")
    public JsonResultDto searchFriend(String keyword) {
        try {
            List<HxUserDto> friendList = sysPartnerService.searchFriend(keyword);
            return success(friendList);
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }

    }

    private void isExistedHxUser(String hxId) {
        HxUserDto userDto = sysPartnerService.queryHxUser(hxId);
        if (null == userDto) {
            throw new RuntimeException(CodeConstants.CODE_103);
        }
    }

}
